package com.tl.commonlibrary.ui.picture.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.view.View;
import android.view.ViewGroup;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.squareup.picasso.Picasso;
import com.tl.commonlibrary.R;
import com.tl.commonlibrary.ui.adapter.IAdapter;
import com.tl.commonlibrary.ui.adapter.ViewHolder;
import com.tl.commonlibrary.ui.picture.bean.Picture;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/6/6 0006.
 * 缩略图查看适配器
 */

public class PictureAdapter extends IAdapter<Picture> {

    public PictureAdapter(Context context, ArrayList<Picture> datas) {
        super(context, datas, R.layout.item_thumbnail);
    }


    @Override
    protected void getView(ViewHolder viewHolder, Picture entity, int position, View convertView, boolean isConvert, int itemViewType, ViewGroup parent) {
        AppCompatImageView imageIView = viewHolder.getView(R.id.imageIView);
        convertView.setTag(R.id.tag_id, entity);
        if (!isConvert) {
            ViewGroup.LayoutParams params = imageIView.getLayoutParams();
            params.width = (int) imageIView.getContext().getResources().getDimension(R.dimen.img_len_02);
            params.height = (int) imageIView.getContext().getResources().getDimension(R.dimen.img_len_02);
        }
        Picasso.with(convertView.getContext()).load(entity.getUri()).placeholder(R.drawable.ic_default).error(R.drawable.ic_default).into(imageIView);
        convertView.setOnClickListener(this);
    }


    /**
     * 拿到列表中的本地路径列表
     *
     * @return
     */
    public ArrayList<LocalMedia> filterLocalMedias() {
        ArrayList<LocalMedia> medias = null;
        int len = getDataSize();
        if (len > 0) {
            medias = new ArrayList<>();
            for (int i = 0; i < len; i++) {
                medias.add(getItem(i).getLocalMedia());
            }
        }
        return medias;
    }

    @Override
    public void onClick(View v) {
        Object object = v.getTag(R.id.tag_id);
        if (object != null && object instanceof Picture) {
            PictureSelector.create((Activity) v.getContext()).externalPicturePreview(mDatas.indexOf(object), filterLocalMedias());
        }
    }
}
